<template>
  <BasicModal :show="show" :title="title" width="30%" :widthComfirm="true" @comfirm="comfirm"
    @closeModal="show = false">
    <template #content>
      <CommonForm :formList="formList" ref="formRef"></CommonForm>
    </template>
  </BasicModal>


</template>

<script setup lang='ts'>
import { ref, watch } from "vue"
import CommonForm from "@/views/applicationDetails/CommonForm.vue";
import BasicModal from "@/views/ApplicationDetails/Modals/BasicModal.vue"
const props = defineProps(['process'])
import type { IndexFormItem } from "@/views/ApplicationDetails/application-type.ts"
import { comStores } from '@/store/common/comStore';
import { interview, trainingArrive, selectTrainingNumberList, selectUserByRole } from "@/api/application-details/index.ts";
import { message } from "ant-design-vue";
const store = comStores()

const { userName } = store.userInFor
const show = ref<boolean>(false);
const showPreview = ref<boolean>(false);
const title = ref<string>("面试改期")
const formList = ref<IndexFormItem[]>([
  {
    label: "面试类型", name: "interviewType", value: "", component: 'select', options: [
      { label: '线上面试', value: "线上面试" },
      { label: '线下面试', value: "线下面试" },
    ]
  },
  { label: "面试时间", name: "appointmentTime", value: "", component: 'datePicker', format: "YYYY-MM-DD HH:mm", showTime: true },
  { label: "面试官", name: "interviewOfficer", value: "", component: 'select', options: [] },
  { label: "面试会议链接", name: "interviewUrl", value: "", component: 'input', required: false },
  { label: "备注", name: "remark", value: "", component: 'textarea', required: false },
])

watch(() => store.currentResume, (newVal) => {
  const val = newVal.processSort
  console.log(val, '===>val')
  if (val == '20') {
    title.value = '培训改期'
    formList.value = [
      {
        label: "培训期数", name: "trainingId", value: "", component: 'cascader', options: []
        // , displayRender: true
      },
      { label: "备注", name: "remark", value: "", component: 'textarea', required: false },
      { label: "发送邮件通知", name: "isSend", value: true, component: 'isSend', required: false },
    ]
  } else if (val == '8') {
    title.value = '面试改期';

  } else if (val == '18') {
    title.value = '培训改期';
    formList.value = [
      {
        label: "培训期数", name: "trainingId", value: "", component: 'cascader', options: []
        // , displayRender: true
      },
      { label: "备注", name: "remark", value: "", component: 'textarea', required: false },
      { label: "发送邮件通知", name: "isSend", value: true, component: 'isSend', required: false },
    ]
  }
  else {
    title.value = '考核改期'
  }
}, { immediate: true });
watch(() => show.value, async (val) => {
  const { processSort } = store.currentResume
  if (val && processSort == 20 || processSort == 18) {
    const data = await selectTrainingNumberList();
    if (data && data.code === '10000') {
      data.data.forEach(l1 => {
        l1.label = l1.dateTime
        l1.value = l1.dateTime
        l1.children = l1.monthList
        l1.monthList.forEach(l2 => {
          l2.label = l2.dateTime
          l2.value = l2.dateTime
          l2.children = l2.numberList
          l2.numberList.forEach(l3 => {
            l3.label = l3.startTimeEndTime + (l3.isOnline == 0 ? ' (未上线)' : "")
            l3.value = l3.trainingNumberId
          })
        })
      })
      formList.value[0].options = data.data
    }
  } else if (show.value && processSort == 8) {
    const data = await selectUserByRole("INTERVIEW");
    console.log(data, 'data')
    if (data && data.code == '10000') {
      formList.value[2].options = data.data
    }
  }
})

const formRef = ref<any>(null)
const formInfo = ref<any>({})
const comfirm = async () => {
  const res = await formRef.value.submitForm();
  const { processSort, resumeId, processId } = store.currentResume;

  let obj = {}
  res.forEach(el => {
    obj[el.name] = el.value
  })

  formInfo.value = obj;
  const text = formInfo.value.interviewUrl || ''
  const regex = /(https?:\/\/[^\s#]+)/;
  const match = text?.match(regex);

  let param: any = {
    resumeId: resumeId,
    processId: processId,
    //"result": "通过",
    interviewType: formInfo.value.interviewType,
    appointmentTime: formInfo.value.appointmentTime,
    interviewOfficer: formInfo.value.interviewOfficer,
    remark: formInfo.value.remark,
    interviewUrl: match ? match[0] : '',
    operator: userName
  }
  let data: any = {}
  if (processSort == '8') {  //面试改期
    data = await interview(param);
  } else if (processSort == '20' || processSort == '18') {
    param = {
      resumeId: resumeId,
      processId: processId,
      trainingId: formInfo.value.trainingId[2],
      remark: formInfo.value.remark,
      isSend: formInfo.value.isSend,
      operator: userName
    }
    data = await trainingArrive(param);
  }

  if (data && data.code === '10000') {
    message.success("改期成功");
    show.value = false;
    showPreview.value = true;
    formList.value.forEach(el => {
      el.value = ""
    })
    store.getResumeInfo(resumeId);
  } else {
    message.error(data.msg)
  }


}







defineExpose({ show })
</script>

<style lang="less" scoped>
.content {
  .items {
    margin-top: 5px;
  }
}

.mt-30 {
  margin-top: 20px !important;
}
</style>
